import json
import os
import sys
from datetime import time

import django
from django.contrib.auth import authenticate
from django.contrib.auth.hashers import check_password
from django.http import JsonResponse

sys.path.insert(0, '../../../')
from django.test import TestCase

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'lovehome_pro.settings.dev')
django.setup()
from xyuzi.models import House, Facility, HouseImage, Order, User, Area

# Create your tests here.
house_id = 5
house = House.objects.get(id=house_id)
# house_images = HouseImage.objects.filter(house_id=house.id)
# orders = Order.objects.filter(house_id=house_id)
# comments = []
# for order in orders:
#     try:
#         user = User.objects.get(id=order.user_id)
#     except Exception as e:
#         print('错误')
#     times = json.dumps(order.update_time, default=str)
#     time = times.replace("'", ' ')
#     comments.append({
#         'comment': order.comment,
#         'ctime': ''.join(times),
#         'user_name': user.username
#     })
# print(comments)
#
# # TODO 多对多查询
# # facilities = house.facility.all()
# # list = []
# # for facilitie in facilities:
# #     list.append(facilitie.id)
# # print(list)
#
# # TODO 多对多增加
# # list = [6, 7, 8]
# # house.facility.add(*list)
# # house.save()
#
# # TODO id查找
# # facility = Facility.objects.filter(id__in=list)
# # if facility:
# #     print(True)
# # img_urls = []
# # for house_image in house_images:
# #     img_urls.append('http://oyucyko3w.bkt.clouddn.com/' + house_image.url)
# # print(house.user.avatar)
# # print(house.user.username)
# # print(house.user_id)
# # print(house.unit)
# # print(house.title)
# # print(house.room_count)
# # print(house.price)
# # print(house.min_days)
# # print(house.max_days)
# # print(img_urls)
# # print(house.deposit)
# # print(comments)
# # """
# # [
# # {
# # 'comment': '满了',
# # 'ctime': datetime.datetime(2019, 10, 18, 15, 35, 43, 505364, tzinfo=<UTC>),
# # 'user_name': '13111111111'
# # },
# # {
# # 'comment': 'bbbbb',
# # 'ctime': datetime.datetime(2019, 10, 18, 15, 53, 17, 470011, tzinfo=<UTC>),
# # 'user_name': '13111111111'
# # }
# # ]
# # """
# # print(house.capacity)
# # print(house.beds)
# # print(house.address)
# # print(house.acreage)
# #
# # str = '"2019-10-18 15:35:43.505364+00:00"'
# # # str.replace('"', '')
# # print(str)
# # response = JsonResponse({
# #     'ctime':str
# # })
# # print(response)
# area_id = 1
# area = Area.objects.get(id=area_id)
# print(area.name)

# user = User.objects.get(id=1)
# user.set_password('123456789')
# user.save()

# mobile = '18511551140'
# password = '123456789'
# user = authenticate(mobile=mobile,
#                     password=password)
# if user:
#     print(True)

# user = User.objects.get(id=4,
#                  real_name__isnull=False,
#                  id_card__isnull=False)
# user = User.objects.filter(id=100)
# print(user)
# # print(user.username)
# House.objects.filter(id=5).update(index_image_url='FpOX1xklRMczqMo9-n-j_tIyOD5w')
# House.objects.filter(id=6).update(index_image_url='FpOX1xklRMczqMo9-n-j_tIyOD5w')
# House.objects.filter(id=7).update(index_image_url='FpOX1xklRMczqMo9-n-j_tIyOD5w')

# House.objects.filter(id=5).update(index_image_url='FpOX1xklRMczqMo9-n-j_tIyOD5w')
# House.objects.filter(id=6).update(index_image_url='FpOX1xklRMczqMo9-n-j_tIyOD5w')
# House.objects.filter(id=7).update(index_image_url='FpOX1xklRMczqMo9-n-j_tIyOD5w')
# create_time = json.dumps(house.create_time, default=str)
# update_time = json.dumps(house.update_time, default=str)
# create_time = house.create_time
# update_time = house.update_time
# print(create_time.date(), type(create_time))
# print((update_time.date() - create_time.date()))
